package org.btik.light.server.platform.context;

import org.btik.context.AppContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 将spring context适配为AppContext
 */
public class LightAppContext implements AppContext, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public <T> T getBean(String beanId) {
        return (T) applicationContext.getBean(beanId);
    }

    @Override
    public <T> List<T> getBeans(Class<T> requiredType) {
        return applicationContext.getBeanProvider(requiredType).stream().collect(Collectors.toList());
    }

    @Override
    public <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}
